﻿using System;
using System.Collections.Generic;
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Linq;
using Tong;
using JModel;


public partial class aspnet_testPargrameDir_site_Member_Admin_xiaoxiong : System.Web.UI.Page
{
    public string str = "";
    /// <summary>
    /// 當前顯示的id
    /// </summary>
    string id = "1";

    /// <summary>
    /// 用於顯示資料
    /// </summary>
    public Tong.TongUse TextData = new Tong.TongUse();

    /// <summary>
    /// 系统安装绝对路径
    /// </summary>
    public string Abs_Path = TongUse.SysPath;
    /// <summary>
    /// 用户存放数据的临时表
    /// </summary>
    DataTable MyDt = new DataTable();

    public string ho = "";

    /// <summary>
    /// 站点通用方法类
    /// </summary>
    public Tong.ThisSiteUse.SiteTong TextThisSite = new Tong.ThisSiteUse.SiteTong();


    //得到传入的栏目ID
    public String CloumnID = "1";

    /// <summary>
    /// 顯示當前欄目的簡介
    /// </summary>
    public string str1 = "";
    /// <summary>
    /// 資料表
    /// </summary>
    DataTable NewDt = new DataTable();
    /// <summary>
    /// 用于分頁的對像
    /// </summary>
    Tong.Fy.FenYe FY = new Tong.Fy.FenYe();
    /// <summary>
    /// 
    /// </summary>
    public string sud = "";

    /// <summary>
    /// 標題
    /// </summary>
    public string title = "";
    /// <summary>
    /// 表名
    /// </summary>
    public string tab = "Sys_WangZhanDeTue3aae";
    /// <summary>
    /// 学生实体
    /// </summary>
    public CX_Student stud = null;

    DBEntities db = new DBEntities();


    /// <summary>
    /// 日期
    /// </summary>
    public DateTime date = DateTime.Now;

    /// <summary>
    /// 学号
    /// </summary>
    public decimal stuid = 0;

    /// <summary>
    /// 班级
    /// </summary>
    public string classNmae = "";


    protected void Page_Load(object sender, EventArgs e)
    {

        //foreach (Control cont in this.爱的叮咛.Parent.Controls)
        //{
        //    Response.Write(cont.ID + "." + cont.GetType().Name + "<br/>");
        //}
        //return;

        stuid = decimal.Parse(Request.QueryString["stid"]);
        date = DateTime.Parse(Request.QueryString["d"]);
        classNmae = Request.QueryString["s"];
        stud = db.CX_Student.Where(p => p.学号 == stuid).FirstOrDefault();
        //如果数据为空则姓名为空
        if (stud == null)
        {
            stud = new CX_Student() { 姓名 = "" };
        }
        if (!IsPostBack)
        {
            //用户模式
            //用户模式下对所有字段进行锁定readonly
            if (!string.IsNullOrEmpty(Request.QueryString["userModel"]))
            {
                foreach (var cont in this.爱的叮咛.Parent.Controls)
                {
                    if (cont is HtmlControl)
                    {
                        HtmlControl htmlcontrol = (HtmlControl)cont;
                        if (htmlcontrol.Disabled)
                        {
                            htmlcontrol.Disabled = false;
                        }
                        else
                        {

                            htmlcontrol.Disabled = true;
                        }
                    }
                }
            }
            #region 如果记录不存在则添加一条新记录到数据库
            CX_Xiaoxiong entity = db.CX_Xiaoxiong.Where(p => p.学号 == stuid && p.日期 == date && p.班级 == classNmae).FirstOrDefault();
            if (entity == null)
            {
                //添加新记录
                entity = new CX_Xiaoxiong()
               {
                   学号 = stuid,
                   日期 = date,
                   班级 = classNmae,

               };
                //添加新记录
                db.CX_Xiaoxiong.AddObject(entity);

                //创建索引
                var dp = db.CX_PingYuIndex.Where(p => p.学号 == entity.学号 && p.评语日期 == entity.日期 && p.班级名称 == entity.班级).FirstOrDefault();
                if (dp == null)
                {
                    //从学生表获取姓名
                    var stentity = db.CX_Student.Where(p => p.学号 == entity.学号).FirstOrDefault();
                    if (stentity != null)
                    {
                        string stname = stentity.姓名 == null ? string.Empty : stentity.姓名;
                        db.CX_PingYuIndex.AddObject(new CX_PingYuIndex()
                        {
                            学号 = stuid,
                            评语日期 = date,
                            班级名称 = classNmae,
                            姓名 = stname
                        });
                    }
                }
            }
            else
            {
                //如果记录存在则帮定数据
                教学主题.Value = entity.教学主题;
                老师的话.Value = entity.老师的话;
                工作概况.Value = entity.工作概况;
                家长反馈.Value = entity.家长反馈;
                家长反馈签名.Value = entity.家长反馈签名;
                爱的叮咛.Value = entity.爱的叮咛;


            }
            db.SaveChanges();
            #endregion
        }

    }

    /// <summary>
    /// 内容状态保存时发生
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    public void Page_SaveStateComplete(object sender, EventArgs e)
    {

        db.Connection.Close();
        db.Dispose();
    }
    /// <summary>
    /// 保存数据
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Button1_Click(object sender, System.EventArgs e)
    {
        CX_Xiaoxiong entity = db.CX_Xiaoxiong.Where(p => p.学号 == stuid && p.日期 == date && p.班级 == classNmae).FirstOrDefault();
        entity.学号 = stuid;
        entity.日期 = date;
        entity.班级 = classNmae;

        entity.教学主题 = 教学主题.Value;

        entity.老师的话 = 老师的话.Value;
        entity.工作概况 = 工作概况.Value;
        entity.家长反馈 = 家长反馈.Value;
        entity.家长反馈签名 = 家长反馈签名.Value;
        entity.爱的叮咛 = 爱的叮咛.Value;

        if (!string.IsNullOrEmpty(Request.QueryString["userModel"]))
        {
            entity.完成 = true;
        }

        //db.CX_Haitun.AddObject(entity);
        db.SaveChanges();
    }
}